package com.fzu.mmdbproxyframework.configurationmanager;

import java.io.InputStream;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.SAXReader;

import com.fzu.mmdbproxyframework.context.ConfigContext;
import com.fzu.mmdbproxyframework.model.DataNode;
import com.fzu.mmdbproxyframework.model.MMDataNodeCluster;
import com.fzu.mmdbproxyframework.model.Rule;


public class RuleConfigRead {
    
	public MMDataNodeCluster cluster;
	 
    /**
	 * 
	 */
	private RuleConfigRead() {
		super();
	}



	/**
	 * @param cluster
	 */
	public RuleConfigRead(MMDataNodeCluster cluster) {
		this.cluster = cluster;
	}



	/**
     * ruleParamList 参数说明
     * 				name=heartbeat 默认  规则的名字 这里是心跳的规则  所以map里面必须是心跳的参数
     * 				keepaliveTime=5000 心跳时间
     *				createTableStr=create table xdual(id int not null,gmt datetime,primary key (id))
     *				insertStr=insert into xdual values(1,now());
     *				updateStr=update xdual set gmt=now() where id=1
     *				selectStr=select 1 from xdual
     *				dropTableIfExitStr=drop table if exists xdual
     *
     *				name = Server 默认    规则的名字 这里是系统的的规则  所以map里面必须是系统的参数
     * 				updatePeriod=3000    内存数据库的全量同步时间
     * 				Master_Slave=true  默认    数据源是否配置主从
     * 				SourceDBNodeType=mysql 默认    数据源的数据类型 
     * 				"MMDBType", "h2  内存数据库类型 默认h2
     * 				MMDBNumbers=3  需要创建几台内存数据库虚拟机
     * 				AllTables=true   是否同步所有表
     * **/
    public  List<Rule> buildRuleList( List<Map<String,String>> ruleParamList){
    	for(Map<String,String> rulemap:ruleParamList){
    		System.out.println("rulemap="+rulemap);
    		Rule rule =new Rule();
    		rule.setProperty(rulemap);
    		cluster.getConfigContext().getRuleList().add(rule);
    	}
    	
    	return cluster.getConfigContext().getRuleList();
    }
	
	
    
    public  List<Rule> buildRule() {
    	 SAXReader reader = new SAXReader(); 
    	 InputStream in = RuleConfigRead.class.getClassLoader().getResourceAsStream("rule.xml");  
         Document doc;
        try {
        	doc = reader.read(in);
            Element root = doc.getRootElement(); 
            if (root == null) return null;
            System.err.println(root.attributeValue("name"));
        
            // all rule node
            List<Element> hostNodes = root.elements();
            if (hostNodes == null) return null;
            System.out.println("hostNodes.size()="+hostNodes.size());
            for(int i = 0; i < hostNodes.size(); i++) {
            	Rule rule =new Rule();
            	Element RuleNod = hostNodes.get(i);               
                if (RuleNod != null && RuleNod.getNodeType() == Node.ELEMENT_NODE) {
                	System.err.println(RuleNod.attributeValue("name"));
//                	rule.setName(RuleNod.attributeValue("name"));               	
                    // all class node
                	List<Element> propertyNodes = RuleNod.elements();
                	Map<String,String> attributeMap = new HashMap<String,String>();
                    for (int j = 0; j < propertyNodes.size(); j++) {
                    	Element property = propertyNodes.get(j);
                        if (property != null && property.getNodeType() == Node.ELEMENT_NODE) {
                        	String str = property.attributeValue("name");
                        	String strvalue = property.getText();
                            System.err.print("\t\t" +str);
                          	System.err.print(" " + strvalue); //   
                          	attributeMap.put(str,strvalue);
                        }
                    }
                    rule.setProperty(attributeMap);
                }
                cluster.getConfigContext().getRuleList().add(rule);
            }
            return cluster.getConfigContext().getRuleList();
        } catch (DocumentException e) {
            e.printStackTrace();
            return null;
        }
      
        
    }
	


}